package com.java.project_2.CustomerView;


import com.java.project_2.CustomerList.CustomerList;
import com.java.project_2.CustomerUtil.CMUtility;
import com.java.project_2.Customerbean.Customer;

/**
 * @author luzhikun
 * @date 2020/11/30- 21:30
 */
public class Customerview {
    public static void main(String[] args) {
        Customerview customerview = new Customerview();
        customerview.enterMainMenu();

    }

    /**
     * 先给创建10个内存空间
     */

    private CustomerList customerList = new CustomerList(10);

    public Customerview() {
        Customer customer = new Customer();
        customerList.addCustomer(customer);
    }

    public void enterMainMenu() {
        boolean isFlag = true;
        while (isFlag) {
            System.out.println("--------客户信息管理软件--------");
            System.out.println("        1.添加客户");
            System.out.println("        2.修改客户");
            System.out.println("        3.删除客户");
            System.out.println("        4.客户列表");
            System.out.println("        5.退出");
            System.out.println("        请选择（1-5）：");
            char menu = CMUtility.readMenuSelection();
            switch (menu) {
                case '1':
                    addNewCustomer();
                    break;
                case '2':
                    deleCustomer();
                    break;
                case '3':
                    modifyCustomer();
                    break;
                case '4':
                    getAllCustomer();
                    break;
                case '5':
                    System.out.println("请确认是否退出(Y/N)");
                    char r = CMUtility.readConfirmSelection();
                    if (r == 'Y') {
                        isFlag = false;
                    }
            }
        }

    }

    public void addNewCustomer() {
        System.out.println("-------------------添加用户------------------");
        System.out.print("请输入姓名");
        String name = CMUtility.readString(10);
        System.out.print("请输入性别");
        char genger = CMUtility.readChar();
        System.out.print("请输入年龄");
        int age = CMUtility.readInt();
        System.out.print("请输入电话号码");
        String phone = CMUtility.readString(13);
        System.out.print("请输入邮箱");
        String email = CMUtility.readString(30);

        //将上述数据封装到一个对象中

        Customer customer = new Customer(name, genger, age, phone, email);
        boolean isFlag = customerList.addCustomer(customer);
        if (isFlag == true) {
            System.out.println("----------------添加成功----------------------");
        } else {
            System.out.println("----------------添加失败-----------------------");

        }
    }

    public void deleCustomer() {
        System.out.println("-------------------删除用户------------------");
        Customer customer;
        int num;
        for (; ; ) {
            System.out.print("请选择你要修改的客户(-1退出)：");
            num = CMUtility.readInt();
            if (num == -1) {
                return;
            } else {
                customer = customerList.getCustomers(num - 1);
                if (customer == null) {
                    System.out.println("没有找到指定客户");
                } else {
                    break;
                }
            }
        }
        System.out.print("请确认是否删除(Y/N)");
        char isFlag = CMUtility.readConfirmSelection();
        if (isFlag == 'Y') {
            boolean dele = customerList.deleCustomer(num - 1);
            if (dele == true) {
                System.out.println("----------------------删除成功-------------------");
            } else
                System.out.println("----------------------删除失败-------------------");
        }


    }

    public void modifyCustomer() {
        System.out.println("--------------------修改用户----------------");
        int num;
        Customer customer;
        for (; ; ) {
            System.out.print("请输入你要修改的客户(-1退出)：");
            num = CMUtility.readInt();
            if (num == -1) {
                return;
            } else {
                customer = customerList.getCustomers(num - 1);
                if (customer == null) {
                    System.out.println("没有找到指定客户");
                } else {
                    break;
                }
            }
        }
        System.out.print("姓名(" + customer.getName() + ")");
        String name = CMUtility.readString(10, customer.getName());
        System.out.print("性别(" + customer.getGender() + ")");
        char genger = CMUtility.readChar(customer.getGender());
        System.out.print("年龄(" + customer.getAge() + ")");
        int age = CMUtility.readInt(customer.getAge());
        System.out.print("电话(" + customer.getPhone() + ")");
        String phone = CMUtility.readString(13, customer.getPhone());
        System.out.print("邮箱(" + customer.getName() + ")");
        String email = CMUtility.readString(30, customer.getEmail());
        Customer newCust = new Customer(name, genger, age, phone, email);
        boolean isFlag = customerList.replCustomer(num - 1, newCust);
        if (isFlag) {
            System.out.println("-----------------修改成功------------------");
        } else {
            System.out.println("-----------------修改失败------------------");
        }
    }

    public void getAllCustomer() {
        System.out.println("-------------------客户列表---------------");
        int num = customerList.getTotal();
        if (num == 0) {
            System.out.println("没有客户");
        } else {
            System.out.print("编号\t姓名\t性别\t年龄\t电话\t\t邮箱");
            Customer[] customer = customerList.getAllCustomers();
            for (int i = 0; i < customer.length; i++) {
                Customer customer1 = customer[i];
                System.out.println((i + 1) + "\t" + customer1.getName() + "\t" + customer1.getGender() + "\t" + customer1.getName() + "\t" + customer1.getPhone() + "\t\t" + customer1.getEmail());
            }
        }
    }

}
